﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Rally_like_a_madman.Data;
using System.Windows.Navigation;

namespace Rally_like_a_madman
{
    public partial class MainPage : PhoneApplicationPage
    {
        public List<User> users;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateRoute_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CreateRoutePage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartRoute_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/StartRoutePage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHighscores_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/HighscoresPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            while (this.NavigationService.BackStack.Any())
            {
                //According to a post on StackOverflow:
                //RemoveBackEntry sometimes throws NullReferenceException. 
                //Some logic inside navigation system is broken so on FAS it sometimes adds phantom journalentries with PageInstance == null.
                try
                {
                    this.NavigationService.RemoveBackEntry();
                }
                catch (Exception)
                { }
            }

            base.OnNavigatedTo(e);
        }
    }
}